package com.woniuxy.kaer.service.consumer;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.kaer.commons.util.Data;
import com.woniuxy.kaer.service.entity.ServiceOrder;
import com.woniuxy.kaer.service.mapper.ServiceOrderMapper;
import org.aspectj.weaver.ast.Var;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.util.HashMap;
import java.util.Map;

@Component
public class JudgeOrderExpiredConsumer {
    @Resource
    private ServiceOrderMapper serviceOrderMapper;
    @RabbitListener(queues = "judge_order_expired_queue")
    @RabbitHandler
    public void judgeOrderExpired(String serviceOrderId)throws Exception{
        System.out.println("judge_order_expired_queue"+serviceOrderId);
        QueryWrapper<ServiceOrder> serviceOrderQueryWrapper = new QueryWrapper<>();
        serviceOrderQueryWrapper.eq("service_order_id",serviceOrderId);
        ServiceOrder serviceOrder = serviceOrderMapper.selectOne(serviceOrderQueryWrapper);
       // ServiceOrder serviceOrder = serviceOrderMapper.selectById(serviceOrderId);
        System.out.println(serviceOrder);
        if(null!=serviceOrder&&serviceOrder.getServiceOrderState()== Data.SERVICE_ORDER_STATE_APPOINTMENT){
            //LocalDateTime appointmentTime =localhost
//            String xxlLoginAPI="http://192.168.50.8:9000/xxl-job-admin/login";
//            String xxlAddJobAPI="http://192.168.50.8:9000/xxl-job-admin/jobinfo/add";
            String xxlLoginAPI="http://192.168.50.8:9000/xxl-job-admin/login";
            String xxlAddJobAPI="http://192.168.50.8:9000/xxl-job-admin/jobinfo/add";
            HashMap<String, Object> loginParam = new HashMap<String,Object>();
            loginParam.put("userName", "admin");
            loginParam.put("password", "123456");
            HttpResponse httpResponse = HttpRequest.post(xxlLoginAPI).form(loginParam).execute();
            if(httpResponse.getStatus()==200) {
                System.out.println("登录成功");
                Map<String, Object> addTaskParam = new HashMap<String,Object>();
                //addTaskParam.put("jobGroup", 5);
                addTaskParam.put("jobGroup", 5);
//                LocalDateTime now = LocalDateTime.now();
//                LocalDateTime executeTime = now.plusMinutes(1);
                LocalDateTime executeTime = serviceOrder.getAppointmentTime();
                int year = executeTime.get(ChronoField.YEAR);
                int month = executeTime.get(ChronoField.MONTH_OF_YEAR);
                int day = executeTime.get(ChronoField.DAY_OF_MONTH);
                int hour=executeTime.get(ChronoField.HOUR_OF_DAY);
                int minute=executeTime.get(ChronoField.MINUTE_OF_HOUR);
                int second=executeTime.get(ChronoField.SECOND_OF_MINUTE);
                StringBuilder cron = new StringBuilder();
                cron.append(second);
                cron.append(" "+minute);
                cron.append(" "+hour);
                cron.append(" "+day);
                cron.append(" "+month);
                cron.append(" ?");
                cron.append(" "+year);
                System.out.println(cron.toString());
                addTaskParam.put("scheduleConf", cron.toString());
                addTaskParam.put("jobDesc", "预约时间超时任务");
                addTaskParam.put("author", "csy");
                addTaskParam.put("executorHandler", "orderExpiredHandler");
                addTaskParam.put("executorParam",serviceOrderId);//测试的拼团编号
                addTaskParam.put("scheduleType", "CRON");
                addTaskParam.put("executorRouteStrategy", "FIRST");
                addTaskParam.put("executorBlockStrategy", "SERIAL_EXECUTION");
                addTaskParam.put("glueType", "BEAN");
                addTaskParam.put("misfireStrategy","DO_NOTHING");
                addTaskParam.put("executorTimeout", "0");
                addTaskParam.put("executorFailRetryCount","0");
                addTaskParam.put("triggerStatus", 1);
                HttpResponse response = HttpRequest.post(xxlAddJobAPI).form(addTaskParam).execute();
                System.out.println(response.body());
                //从body中取出content content代表任务ID
                //使用redis保存拼团编号-任务ID键值对
                if(response.getStatus()!=200) {
                    throw new Exception("预约时间超时任务失败");
                }
            }

        }
    }
}
